package org.bluedream.comm.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.UUID;

public class CreateGUIDUtil {
    private CreateGUIDUtil(){
        throw new AssertionError();
    }

    public static final String GenerateGUID(){
        UUID uuid = UUID.randomUUID();
        String guid = uuid.toString();
        return guid.replace("-" , "");
    }

    public static final String nextCode(String code) {
        if (code == null) {
            return null;
        } else {
            String str = code.trim();
            int len = str.length() - 1;
            int lastNotNumIndex = 0;

            for(int i = len; i >= 0; --i) {
                if (str.charAt(i) < '0' || str.charAt(i) > '9') {
                    lastNotNumIndex = i;
                    break;
                }
            }

            if (str.charAt(len) >= '0' && str.charAt(len) <= '9' && lastNotNumIndex == len) {
                lastNotNumIndex = -1;
            }

            String prefix = str.substring(0, lastNotNumIndex + 1);
            String numStr = str.substring(lastNotNumIndex + 1, str.length());
            long num = toLong(numStr);
            str = prefix + org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(num + 1L), numStr.length(), "0");
            return str;
        }
    }

    public static final Long toLong(Object val) {
        return toDouble(val).longValue();
    }

    public static final Double toDouble(Object val) {
        if (val == null) {
            return 0.0D;
        } else {
            try {
                return NumberUtils.toDouble(StringUtils.trim(val.toString()));
            } catch (Exception var2) {
                return 0.0D;
            }
        }
    }
}
